package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.service.WechatService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WechatServiceImpl implements WechatService {
    //获取openId
    public static final  String URL_OPENID="https://api.weixin.qq.com/sns/jscode2session ";
    //获取getAccessToken地址
    public static final String URL_ACCESSTOKEN="https://api.weixin.qq.com/cgi-bin/token";
    //获取手机号地址
    public static final String URL_PHONE="https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";

    @Value("${weChat.appId}")
    private String appid;

    @Value("${weChat.appSecret}")
    private String appSecret;

    /**
     * 获取openid
     * @param code
     * @return
     */
    @Override
    public String getOpenid(String code) {
        //封装请求参数
        Map<String,Object> dataMap = getConfigApi();
        dataMap.put("js_code",code);
        dataMap.put("grant_type","authorization_code");
        //使用hutool的httpUtil发送get请求
        String result = HttpUtil.get(URL_OPENID, dataMap);
        //获取响应结果，分析结果
        JSONObject jsonObject = JSONUtil.toBean(result, JSONObject.class);
        //errcode=0,可以获取openid
        String openId = jsonObject.getStr("openid");
        if(ObjectUtil.isEmpty(openId)){
            throw new BaseException("获取openid异常");
        }
        return openId;
    }
    @NotNull
    private Map<String, Object> getConfigApi() {
        HashMap<String, Object> dataMap = new HashMap<>();
        dataMap.put("appid",appid);
        dataMap.put("secret",appSecret);
        return dataMap;
    }

    /**
     * 获取手机号
     * @param code
     * @return
     */
    @Override
    public String getPhone(String code) {
        //定义参数
        HashMap<String, Object> dataMap = new HashMap<>();
        dataMap.put("code",code);
        //执行post请求
        HttpResponse response = HttpUtil.createPost(URL_PHONE + getAccessToken())
                .body(JSONUtil.toJsonStr(dataMap), "application/json;charset=utf-8")
                .execute();
        //解析数据，获取手机号
        String result = response.body();
        JSONObject jsonObject = JSONUtil.toBean(result, JSONObject.class);
        if(jsonObject.getInt("errcode")!=0){
            throw new BaseException("获取手机号失败");
        }
        Object phone = jsonObject.getByPath("phone_info.purePhoneNumber");
        return phone.toString();
    }

    /**
     * 获取accessToken
     * @return
     */
    private String getAccessToken() {
        //定义参数
        HashMap<String, Object> dataMap = new HashMap<>();
        dataMap.put("grant_type","client_credential");
        dataMap.put("appid","wx3d69715e1dfab27f");
        dataMap.put("secret","dd3e5e298ccbd46bb56da0b7c7d9a9a2");
        //执行get请求
        String result = HttpUtil.get(URL_ACCESSTOKEN, dataMap);
        //解析数据
        JSONObject jsonObject = JSONUtil.toBean(result, JSONObject.class);
        String accessToken = jsonObject.getStr("access_token");
        if(ObjectUtil.isEmpty(accessToken)){
            throw new BaseException("获取accessToken失败");
        }
        return accessToken;
    }
}
